package hys.wssoc.modules.info.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hys.wssoc.common.utils.PageUtils;
import hys.wssoc.common.utils.Query;
import hys.wssoc.modules.constant.RedisConstants;
import hys.wssoc.modules.info.dao.CountrysideDao;
import hys.wssoc.modules.info.dao.ProvinceDao;
import hys.wssoc.modules.info.entity.ProvinceEntity;
import hys.wssoc.modules.info.service.IRedisService;
import hys.wssoc.modules.info.service.ProvinceService;
import hys.wssoc.modules.info.utils.CommonUtils;
import hys.wssoc.modules.info.vo.ProvinceDetailVo;
import hys.wssoc.modules.info.vo.ProvinceSummaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("provinceService")
public class ProvinceServiceImpl extends ServiceImpl<ProvinceDao, ProvinceEntity> implements ProvinceService {

    @Autowired
    private ProvinceDao provinceDao;

    @Autowired
    private CountrysideDao countrysideDao;

    @Autowired
    private IRedisService redisService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProvinceEntity> page = this.page(
                new Query<ProvinceEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProvinceSummaryVo> selectList() {
        List<ProvinceEntity> summaryList;
        if (redisService.exists(RedisConstants.PROVINCE + "summaryList")){
            String redisValue = redisService.get(RedisConstants.PROVINCE + "summaryList");
            summaryList = JSON.parseArray(redisValue, ProvinceEntity.class);
        }else {
            QueryWrapper<ProvinceEntity> wrapper = new QueryWrapper<>();
            wrapper.select("province_name", "ratio", "countryside_num", "rsc_num");
            summaryList = provinceDao.selectList(wrapper);
            redisService.set(RedisConstants.PROVINCE + "summaryList", JSON.toJSONString(summaryList), 86400L);
        }
        // 将数据库返回值赋值给VO对象返回给Controller层
        return summaryList.size() == 0 ? new ArrayList<>() :CommonUtils.transformClass4List(summaryList, ProvinceSummaryVo.class);
    }

    @Override
    public ProvinceDetailVo detail(String provinceName) {
        ProvinceDetailVo provinceDetail;
        if(redisService.exists(RedisConstants.PROVINCE + "detail_" + provinceName)){
            String redisValue = redisService.get(RedisConstants.PROVINCE + "detail_" + provinceName);
            provinceDetail = JSON.parseObject(redisValue, ProvinceDetailVo.class);
        } else {
            QueryWrapper<ProvinceEntity> wrapper = new QueryWrapper<>();
            wrapper.select("province_name", "city_num", "countryside_num", "ratio", "rsc_num");
            wrapper.eq("province_name", provinceName);
            ProvinceEntity province = provinceDao.selectOne(wrapper);
            provinceDetail = CommonUtils.transformClass(province, ProvinceDetailVo.class);

            List<Map<String, Long>> map = countrysideDao.selectNumByGeo(provinceName);
            Map<Long, Long> result = new HashMap<>();
            // 这里查出来count是Long类型
            map.forEach(m->result.put(m.get("rs_time"), m.get("num")));

            provinceDetail.setRscNum_1(result.getOrDefault(1, 0L));
            provinceDetail.setRscNum_2(result.getOrDefault(2, 0L));
            provinceDetail.setRscNum_3(result.getOrDefault(3, 0L));
            provinceDetail.setRscNum_4(result.getOrDefault(4, 0L));
            provinceDetail.setRscNum_5(result.getOrDefault(5, 0L));
            provinceDetail.setRscNum_6(result.getOrDefault(6, 0L));
            redisService.set(RedisConstants.PROVINCE + "detail_" + provinceName, JSON.toJSONString(provinceDetail), 86400L);
        }
        return provinceDetail;
    }

}